package cn.com.xinjinfu.pdoulcProduct.fragment.AboutPDou;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.SalfControlInfoAct;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.AboutPdouBean;
import cn.com.xinjinfu.pdoulcProduct.entity.AboutPdouBean.MyPdouMap.Safetys;
import cn.com.xinjinfu.pdoulcProduct.entity.HomeBean;
import cn.com.xinjinfu.pdoulcProduct.fragment.BaseFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;
import cn.com.xinjinfu.pdoulcProduct.view.ListViewForScrollView;

/**
 * 安全保障
 * Created by android-adt on 2015/12/31.
 */
public class SalfFragment extends Fragment {
    private View viewFragment;
    private Activity mActivity;
    private RequestQueue requen;
    private ListViewForScrollView my_listview;
    private MyAdapter myAdapter;
    private AboutPdouBean selfBean;
    private ArrayList<Safetys> safetys;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewFragment = inflater.inflate(R.layout.fragment_safe, null);
        return viewFragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        my_listview = (ListViewForScrollView) view.findViewById(R.id.my_listview);
        myAdapter = new MyAdapter(mActivity);
        my_listview.setAdapter(myAdapter);
        my_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, SalfControlInfoAct.class);
                Bundle bundle = new Bundle();
                bundle.putString("url", safetys.get(position).getLinkUrl());
                bundle.putString("adress", "salf");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requen = Volley.newRequestQueue(mActivity);
        loginRequest();
    }

    protected void initData() {
        safetys = selfBean.getMyPdouMap().getSafetys();
        if (!(safetys==null||safetys.equals("null")||safetys.size()<1)){
            myAdapter.setList(safetys);
            myAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 网络请求
     */
    private void loginRequest() {
        final Map<String, String> parameters = DataHandler.getParameters("1423");
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
        requen.add(request);
    }
    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (SharedPreferencesUtils.getUserInfo(mActivity, "selfBean") != "") {
                selfBean = JSON.parseObject(SharedPreferencesUtils.getUserInfo(mActivity, "selfBean"), AboutPdouBean.class);
                if (selfBean != null) {
                    initData();
                }
            }
        }
    };
    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            SharedPreferencesUtils.saveUserInfo(mActivity, "selfBean", jobj.toString());
            selfBean = JSON.parseObject(jobj.toString(), AboutPdouBean.class);
            if (selfBean != null) {
                initData();
            }
        }
    };


    class MyAdapter extends BaseAdapter {
        private Activity context;
        private ArrayList<Safetys> list;
        private LayoutInflater inflater;

        public void setList(ArrayList<Safetys> titles) {
            this.list = titles;
        }

        public MyAdapter(Activity context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_salf_list, null);
                holder.text1 = (TextView) convertView.findViewById(R.id.textView1);
                holder.text2 = (TextView) convertView.findViewById(R.id.textView2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Safetys safetys = list.get(position);
            if (safetys != null) {
                holder.text1.setText(safetys.getTitle());
                holder.text2.setText(safetys.getDescribe());
            }
            return convertView;
        }

        class ViewHolder {
            public TextView text1;
            public TextView text2;
        }
    }

}
